home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / gnu / gnulib / ohlutil / savedir.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-08-24  |  4.1 KB  |  162 lines

  1. /* savedir.c -- save the list of files in a directory in a string
  2.    Copyright (C) 1990 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 1, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* Written by David MacKenzie <djm@ai.mit.edu>. */
  19.  
  20. /*
  21.  * MS-DOS port (c) 1990 by Thorsten Ohl, td12@ddagsi3.bitnet
  22.  *
  23.  * To this port, the same copying conditions apply as to the
  24.  * original release.
  25.  *
  26.  * IMPORTANT:
  27.  * This file is not identical to the original GNU release!
  28.  * You should have received this code as patch to the official
  29.  * GNU release.
  30.  *
  31.  * MORE IMPORTANT:
  32.  * This port comes with ABSOLUTELY NO WARRANTY.
  33.  *
  34.  * $Header: e:/gnu/fileutil/RCS/savedir.c'v 1.3.0.2 90/06/29 00:47:10 tho Stable $
  35.  */
  36.  
  37. #include <sys/types.h>
  38.  
  39. #ifdef MSDOS
  40.  
  41. #include <stdio.h>
  42. extern char *savedir (char *dir, unsigned name_size);
  43. static char *stpcpy (char *dest, char *source);
  44.  
  45. #include "msd_dir.h"
  46. #define NLENGTH(direct) ((direct)->d_namlen)
  47.  
  48. #else /* not MSDOS */
  49.  
  50. #ifdef DIRENT
  51. #include <dirent.h>
  52. #define direct dirent
  53. #define NLENGTH(direct) (strlen((direct)->d_name))
  54. #else
  55. #define NLENGTH(direct) ((direct)->d_namlen)
  56. #ifdef USG
  57. #ifdef SYSNDIR
  58. #include <sys/ndir.h>
  59. #else
  60. #include <ndir.h>
  61. #endif
  62. #else
  63. #include <sys/dir.h>
  64. #endif
  65. #endif
  66.  
  67. #endif /* !MSDOS */
  68.  
  69. #ifdef STDC_HEADERS
  70. #include <stdlib.h>
  71. #include <string.h>
  72. #else
  73. char *malloc ();
  74. char *realloc ();
  75. int strlen ();
  76. #ifndef NULL
  77. #define NULL 0
  78. #endif
  79. #endif
  80.  
  81. char *stpcpy ();
  82.  
  83. /* Return a freshly allocated string containing the filenames
  84.    in directory DIR, separated by '\0' characters;
  85.    the end is marked by two '\0' characters in a row.
  86.    NAME_SIZE is the number of bytes to initially allocate
  87.    for the string; it will be enlarged as needed.
  88.    Return NULL if DIR cannot be opened or if out of memory. */
  89.  
  90. char *
  91. savedir (dir, name_size)
  92.      char *dir;
  93.      unsigned name_size;
  94. {
  95.   DIR *dirp;
  96.   struct direct *dp;
  97.   char *name_space;
  98.   char *namep;
  99.  
  100.   dirp = opendir (dir);
  101.   if (dirp == NULL)
  102.     return NULL;
  103.  
  104. #ifdef MSDOS                 /* stat () it ourselves ... */
  105.   name_size = 0;
  106.   for (dp = readdir (dirp); dp != NULL; dp = readdir (dirp))
  107.     name_size += strlen(dp->d_name) + 1;
  108.   seekdir(dirp, 0L);
  109. #endif /* MSDOS */
  110.  
  111.   name_space = (char *) malloc (name_size);
  112.   if (name_space == NULL)
  113.     return NULL;
  114.   namep = name_space;
  115.  
  116.   while ((dp = readdir (dirp)) != NULL)
  117.     {
  118.       /* Skip "." and ".." (some NFS filesystems' directories lack them). */
  119.       if (dp->d_name[0] != '.'
  120.       || (dp->d_name[1] != '\0'
  121.           && (dp->d_name[1] != '.' || dp->d_name[2] != '\0')))
  122.     {
  123.       unsigned size_needed = (namep - name_space) + NLENGTH (dp) + 2;
  124.  
  125.       if (size_needed > name_size)
  126.         {
  127.           char *new_name_space;
  128.  
  129.           while (size_needed > name_size)
  130.         name_size += 1024;
  131.  
  132.           new_name_space = realloc (name_space, name_size);
  133.           if (new_name_space == NULL)
  134.         {
  135.           closedir (dirp);
  136.           return NULL;
  137.         }
  138.           namep += new_name_space - name_space;
  139.           name_space = new_name_space;
  140.         }
  141.       namep = stpcpy (namep, dp->d_name) + 1;
  142.     }
  143.     }
  144.   *namep = '\0';
  145.   closedir (dirp);
  146.   return name_space;
  147. }
  148.  
  149. /* Copy SOURCE into DEST, stopping after copying the first '\0', and
  150.    return a pointer to the '\0' at the end of DEST;
  151.    in other words, return DEST + strlen (SOURCE). */
  152.  
  153. char *
  154. stpcpy (dest, source)
  155.      char *dest;
  156.      char *source;
  157. {
  158.   while ((*dest++ = *source++) != '\0')
  159.     /* Do nothing. */ ;
  160.   return dest - 1;
  161. }
  162.